/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdListadoServicios.java
 *
 * Created on 12/02/2011, 01:19:41 AM
 */

package com.fsl.sicotec.UI;
import com.fsl.sicotec.Logica.LoServicio;
import com.fsl.sicotec.comun.Entidades.Servicio;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;
import javax.swing.DefaultListSelectionModel;
import com.fsl.sicotec.comun.Entidades.Utiles;
/**
 *
 * @author User
 */
public class jdListadoServicios extends javax.swing.JDialog {

private Utiles uti=null;
    private LoServicio loservicio;
    private DefaultTableModel mdlServicios;
    private ArrayList<Servicio > lista;
    /** Creates new form jdListadoServicios */
    public jdListadoServicios(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
uti=new Utiles();


           //Instanciando las variables
        loservicio = new LoServicio();
        lista = new ArrayList();
        mdlServicios = (DefaultTableModel) jTable1.getModel();
    jTable1.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
        mostrarservicio();
    }
     public void mostrarservicio() {
        int nregistros = loservicio.mostrarServicio().size();
        mdlServicios.setRowCount(nregistros);
        lblNroRegistros.setText("Número de Registros : " + nregistros);
        lista = (ArrayList<Servicio>) loservicio.mostrarServicio();
        int i = 0;
        for (Servicio objServicio : lista) {
            mdlServicios.setValueAt(objServicio.getCod_servicio(), i, 0);
            mdlServicios.setValueAt(objServicio.getVdescripcion(), i, 1);
            mdlServicios.setValueAt(objServicio.getDprecio(), i, 2);
            i++;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        btnNuevo = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnImprimir = new javax.swing.JButton();
        btnCerrar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        lblNroRegistros = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jToolBar1.setRollover(true);

        btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/fsl/sicotec/UI/Recursos/agregar.png"))); // NOI18N
        btnNuevo.setText("Nuevo");
        btnNuevo.setFocusable(false);
        btnNuevo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnNuevo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });
        jToolBar1.add(btnNuevo);

        btnModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/fsl/sicotec/UI/Recursos/editar.png"))); // NOI18N
        btnModificar.setText("Modificar");
        btnModificar.setFocusable(false);
        btnModificar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnModificar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnModificar);

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/fsl/sicotec/UI/Recursos/eliminar.png"))); // NOI18N
        btnEliminar.setText("Eliminar");
        btnEliminar.setFocusable(false);
        btnEliminar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEliminar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnEliminar);

        btnImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/fsl/sicotec/UI/Recursos/PrintHS.png"))); // NOI18N
        btnImprimir.setText("Imprimir");
        btnImprimir.setFocusable(false);
        btnImprimir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnImprimir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btnImprimir);

        btnCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/fsl/sicotec/UI/Recursos/exit.png"))); // NOI18N
        btnCerrar.setText("Cerrar");
        btnCerrar.setFocusable(false);
        btnCerrar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCerrar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnCerrar);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Codigo", "Descripcion", "Precio"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        lblNroRegistros.setText("Nro Registros");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 790, Short.MAX_VALUE)
                .addGap(10, 10, 10))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblNroRegistros)
                .addContainerGap(725, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 496, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addComponent(lblNroRegistros))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        jdServicio vistaServicio = new jdServicio(this, true);
        vistaServicio.setLocationRelativeTo(this);
        vistaServicio.setSize(485, 230);
        vistaServicio.setVisible(true);
        mostrarservicio();
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
         int fila = jTable1.getSelectedRow();
        int cod_servicio;
        if (fila != -1) {
            cod_servicio = Integer.parseInt(mdlServicios.getValueAt(fila, 0).toString());
            jdServicio jdSer = new jdServicio(this, true, cod_servicio);
            jdSer.setLocationRelativeTo(this);
            jdSer.setVisible(true);
            mostrarservicio();

        }







}//GEN-LAST:event_btnModificarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
  int fila = jTable1.getSelectedRow();
        int cod_servicio;
        String servicio;
        if (fila != -1) {
            cod_servicio = Integer.parseInt(mdlServicios.getValueAt(fila, 0).toString());
             servicio = mdlServicios.getValueAt(fila, 1).toString();
            int result = javax.swing.JOptionPane.showConfirmDialog(null,"¿Esta seguro de eliminar el servicio : \n" +  servicio  + "?");
            if(result == 0){
                loservicio.elimarServicio(cod_servicio, "u0025", "My PC");
                javax.swing.JOptionPane.showMessageDialog(null,"Cliente eliminado correctamente");
                mostrarservicio();
            }
        }
}//GEN-LAST:event_btnEliminarActionPerformed

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        setVisible(false);
}//GEN-LAST:event_btnCerrarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                jdListadoServicios dialog = new jdListadoServicios(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnImprimir;
    private javax.swing.JButton btnModificar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel lblNroRegistros;
    // End of variables declaration//GEN-END:variables

}
